<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/26
 * Time: 17:48
 */

namespace app\zucai\home;

use app\index\controller\Home;
use think\Db;
class Money extends Home
{

    public function index(){
        $date=Db::name('order')->where('isdraw',1)->select();

        foreach ($date as $k=>$v){

            $date[$k]['choice']=explode(',',$v['choice']);
            foreach ( $date[$k]['choice'] as $k1=>$v1){
                $date[$k]['choice'][$k1]=explode('=>',$v1);
                $date[$k]['choice'][$k1][1]=explode('-',$date[$k]['choice'][$k1][1]);

                    foreach ($date[$k]['choice'][$k1][1] as $k3=>$v3){
                        $date[$k]['choice'][$k1][1][$k3]=explode('@',$v3);
                        $date[$k]['choice'][$k1][1][$k3][1]=explode('|',$date[$k]['choice'][$k1][1][$k3][1]);

                    }

            }

            $con=$date[$k]['choice'];
            foreach ($con as $key=>$value){
                $flag[$key]=[];
                $cont=Db::name('kaijiang')->where('play_id',$value[0])->find();
                if($cont){
                    foreach ($value[1] as $key1=>$value1){
                        if($value1[0]=='win'){   //判断胜负平中奖
                            if(in_array($cont['win'],$value1[1])){
                                $flag[$key][]=$cont['win_money'];
                            }
                        }
                        if($value1[0]=='ran'){//判断让球胜负平中奖
                            if(in_array($cont['rwin'],$value1[1])){
                                $flag[$key][]=$cont['rwin_money'];
                            }
                        }
                        if($value1[0]=='fen'){//判断比分中奖
                            if(in_array($cont['score'],$value1[1])){
                                $flag[$key][]=$cont['score_money'];
                            }
                        }
                        if($value1[0]=='all'){ //判断总进球数中奖
                            if(in_array($cont['goal'],$value1[1])){
                                $flag[$key][]=$cont['goal_money'];
                            }
                        }
                        if($value1[0]=='half'){ //判断半全场胜负平中奖
                            if(in_array($cont['doub'],$value1[1])){
                                $flag[$key][]=$cont['double_money'];
                            }
                        }

                    }

                }else{
                    return '暂未开奖';
                }

            }
            $price=Db::name('playrule')->field('money')->where('cid',1)->find();
            $price=$price['money'];
            $type=$date[$k]['choicetype'];$bei=$date[$k]['beishu'];
            $arr=[]; $array=[];

            if(count($flag)==1){ //单场
                for($k1=0;$k1<count($flag[0]);$k1++) {
                    $arr[] = round($flag[0][$k1] * $price * $bei, 2);
                }
            }elseif (count($flag)==2){ //2场
                if($type=='2串1'){
                            for($k1=0;$k1<count($flag[0]);$k1++) {
                                for ($kv = 0; $kv < count($flag[1]); $kv++) {
                                    $arr[] = round($flag[0][$k1] * $flag[1][$kv]* $price* $bei, 2);
                                }
                            }
                }else{
                    return '投注方式错误';
                }

            }elseif (count($flag)==3){
                for ($k1 = 0; $k1 < count($flag[0]); $k1++) {  //3场3串1
                    for ($kv = 0; $kv < count($flag[1]); $kv++) {
                        for ($kv3 = 0; $kv3 < count($flag[2]); $kv3++) {
                            $array[] = round($flag[0][$k1] * $flag[1][$kv]*$flag[2][$kv3] * $price* $bei ,2);
                        }
                    }
                }
                $arr1= $this->two($flag,$bei,$price);
                if($type=='3串1'){
                    $arr=$array;
                }elseif($type=='3串3') {
                    $arr = $arr1;
                }elseif($type=='3串4'){ //3场3串4
                        $arr=array_merge($array,$arr1);
                }else{
                    return '投注方式错误';
                }
            }
            elseif (count($flag)==4){

                for ($k1 = 0; $k1 < count($flag[0]); $k1++) {  //4场4串1
                    for ($kv = 0; $kv < count($flag[1]); $kv++) {
                        for ($kv3 = 0; $kv3 < count($flag[2]); $kv3++) {
                            for ($kv4 = 0; $kv4 < count($flag[3]); $kv4++) {
                                $array[] =round($flag[0][$k1] * $flag[1][$kv] * $flag[2][$kv3]* $flag[3][$kv4] * $price* $bei ,2);
                            }
                        }
                    }
                }

                $arr1= $this->tree($flag,$bei,$price); //4场4串4
                $arr2= $this->two($flag,$bei,$price); //4场4串6

                if($type=='4串1'){   //4场4串1
                    $arr=$array;
                }elseif ($type=='4串4') { //4场4串4
                    $arr=$arr1;

                }elseif($type=='4串6'){ //4场4串6

                    $arr=$arr2;
                }elseif($type=='4串5'){ //4场4串5
                    $arr=array_merge($array,$arr1);
                }elseif($type=='4串11') { //4场4串11
                    $arr = array_merge($array, $arr1);
                    $arr = array_merge($arr, $arr2);
                }else{
                    return '投注方式错误';
                }
            }elseif (count($flag)==5){
                for ($k1 = 0; $k1 < count($flag[0]); $k1++) {  //4场4串1
                    for ($kv = 0; $kv < count($flag[1]); $kv++) {
                        for ($kv3 = 0; $kv3 < count($flag[2]); $kv3++) {
                            for ($kv4 = 0; $kv4 < count($flag[3]); $kv4++) {
                                for ($kv5 = 0; $kv5 < count($flag[4]); $kv5++) {
                                    $array[] = round($flag[0][$k1] * $flag[1][$kv] * $flag[2][$kv3] * $flag[3][$kv4] * $flag[4][$kv5]* $price* $bei, 2);
                                }
                            }
                        }
                    }
                }
                $arr1= $this->four($flag,$bei,$price);//5串5  --4个1组
                $arr2= $this->two($flag,$bei,$price); //5串10  --2个1组
                $arr3= $this->tree($flag,$bei,$price); //5串16  --3个1组


                if ($type=='5串1') {   //5场5串1
                    $arr=$array;
                }elseif ($type=='5串5'){//5场5串5
                    $arr=$arr1;
                }elseif ($type=='5串6') {  //5场5串6
                    $arr=array_merge($array,$arr1);
                }elseif ($type=='5串10') {  //5场5串10
                    $arr=$arr2;
                }elseif ($type=='5串16') {  //5场5串16
                    $arr=array_merge($array,$arr1);
                    $arr=array_merge($arr,$arr3);
                }elseif ($type=='5串20') {  //5场5串20
                    $arr=array_merge($arr2,$arr3);
                }elseif ($type=='5串26') {  //5场5串26
                    $arr=array_merge($array,$arr1);
                    $arr=array_merge($arr,$arr3);
                    $arr=array_merge($arr,$arr2);
                }else{
                    return '投注方式错误';
                }
            }
            elseif (count($flag)==6){
                for ($k1 = 0; $k1 < count($flag[0]); $k1++) {  //4场4串1
                    for ($kv = 0; $kv < count($flag[1]); $kv++) {
                        for ($kv3 = 0; $kv3 < count($flag[2]); $kv3++) {
                            for ($kv4 = 0; $kv4 < count($flag[3]); $kv4++) {
                                for ($kv5 = 0; $kv5 < count($flag[4]); $kv5++) {
                                    for ($kv6 = 0; $kv6 < count($flag[5]); $kv6++) {
                                        $array[] = round($flag[0][$k1] * $flag[1][$kv] * $flag[2][$kv3] * $flag[3][$kv4] * $flag[4][$kv5]* $flag[5][$kv6] * $price* $bei, 2);
                                    }
                                }
                            }
                        }
                    }
                }
                $arr1= $this->two($flag,$bei,$price); //6串15  --2个1组
                $arr2= $this->tree($flag,$bei,$price); //6串20  --3个1组
                $arr3= $this->four($flag,$bei,$price); //      --4个1组
                $arr4= $this->five($flag,$bei,$price);//6串6  --5个1组

                if ($type=='6串1') {   //6场6串1
                    $arr=$array;
                }elseif ($type=='6串6'){//6场6串6
                    $arr=$arr4;
                }elseif ($type=='6串7') {  //6场6串7
                    $arr=array_merge($array,$arr4);
                }elseif ($type=='6串15') {  //6场6串15
                    $arr=$arr1;
                }elseif ($type=='6串20') {  //6场6串20
                    $arr=$arr2;
                }elseif ($type=='6串22') {  //6场6串22
                    $arr=array_merge($array,$arr4);
                    $arr=array_merge($arr,$arr3);
                }elseif ($type=='6串35') {  //6场6串35
                    $arr=array_merge($arr1,$arr2);
                }elseif ($type=='6串42') {  //6场6串42
                    $arr=array_merge($array,$arr4);
                    $arr=array_merge($arr,$arr3);
                    $arr=array_merge($arr,$arr2);
                }elseif ($type=='6串50') {  //6场6串50
                    $arr=array_merge($arr1,$arr2);
                    $arr=array_merge($arr,$arr3);
                }elseif ($type=='6串57') {  //6场6串57
                    $arr=array_merge($array,$arr4);
                    $arr=array_merge($arr,$arr3);
                    $arr=array_merge($arr,$arr2);
                    $arr=array_merge($arr,$arr1);
                }else{
                    return '投注方式错误';
                }
            }elseif (count($flag)==7){
                for ($k1 = 0; $k1 < count($flag[0]); $k1++) {  //4场4串1
                    for ($kv = 0; $kv < count($flag[1]); $kv++) {
                        for ($kv3 = 0; $kv3 < count($flag[2]); $kv3++) {
                            for ($kv4 = 0; $kv4 < count($flag[3]); $kv4++) {
                                for ($kv5 = 0; $kv5 < count($flag[4]); $kv5++) {
                                    for ($kv6 = 0; $kv6 < count($flag[5]); $kv6++) {
                                        for ($kv7 = 0; $kv7 < count($flag[6]); $kv7++) {
                                            $array[] = round($flag[0][$k1] * $flag[1][$kv] * $flag[2][$kv3] * $flag[3][$kv4] * $flag[4][$kv5] * $flag[5][$kv6] * $flag[6][$kv7] * $price* $bei, 2);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                $arr1= $this->two($flag,$bei,$price);  // --2个1组
                $arr2= $this->tree($flag,$bei,$price); //  --3个1组
                $arr3= $this->four($flag,$bei,$price); //  --4个1组
                $arr4= $this->five($flag,$bei,$price);//  --5个1组
                $arr5= $this->six($flag,$bei,$price);//  --6个1组

                if ($type=='7串1') {   //7场7串1
                    $arr=$array;
                }elseif ($type=='7串7'){//7场7串7
                    $arr=$arr5;
                }elseif ($type=='7串8') {  //7场7串8
                    $arr=array_merge($array,$arr5);
                }elseif ($type=='7串21') {  //7场7串21
                    $arr=$arr4;
                }elseif ($type=='7串35') {  //7场7串35
                    $arr=$arr3;
                }elseif ($type=='7串22') {  //7场7串22
                    $arr=array_merge($array,$arr4);
                    $arr=array_merge($arr,$arr3);
                }elseif ($type=='7串35') {  //7场7串35
                    $arr=array_merge($arr1,$arr2);
                }elseif ($type=='7串42') {  //7场7串42
                    $arr=array_merge($array,$arr5);
                    $arr=array_merge($arr,$arr4);
                    $arr=array_merge($arr,$arr3);
                    $arr=array_merge($arr,$arr2);
                    $arr=array_merge($arr,$arr1);
                }else{
                    return '投注方式错误';
                }
            }
            elseif (count($flag)==8){
                for ($k1 = 0; $k1 < count($flag[0]); $k1++) {  //8场8串1
                    for ($kv = 0; $kv < count($flag[1]); $kv++) {
                        for ($kv3 = 0; $kv3 < count($flag[2]); $kv3++) {
                            for ($kv4 = 0; $kv4 < count($flag[3]); $kv4++) {
                                for ($kv5 = 0; $kv5 < count($flag[4]); $kv5++) {
                                    for ($kv6 = 0; $kv6 < count($flag[5]); $kv6++) {
                                        for ($kv7 = 0; $kv7 < count($flag[6]); $kv7++) {
                                            for ($kv8 = 0; $kv8 < count($flag[7]); $kv8++) {
                                                $array[] = round($flag[0][$k1] * $flag[1][$kv] * $flag[2][$kv3] * $flag[3][$kv4] * $flag[4][$kv5] * $flag[5][$kv6] * $flag[6][$kv7]* $flag[7][$kv8] * $price* $bei, 2);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                $arr1= $this->two($flag,$bei,$price);  // --2个1组
                $arr2= $this->tree($flag,$bei,$price); //  --3个1组
                $arr3= $this->four($flag,$bei,$price); //  --4个1组
                $arr4= $this->five($flag,$bei,$price);//  --5个1组
                $arr5= $this->six($flag,$bei,$price);//  --6个1组
                $arr6= $this->seven($flag,$bei,$price);//  --7个1组

                if ($type=='8串1') {   //8场8串1
                    $arr=$array;
                }elseif ($type=='8串8'){//8场8串8
                    $arr=$arr6;
                }elseif ($type=='8串9') {  //8场8串9
                    $arr=array_merge($array,$arr6);
                }elseif ($type=='8串28') {  //8场8串28
                    $arr=$arr5;
                }elseif ($type=='8串56') {  //8场8串56
                    $arr=$arr4;
                }elseif ($type=='8串70') {  //8场8串70
                    $arr=$arr3;
                }elseif ($type=='8串247') {  //8场8串247
                    $arr=array_merge($array,$arr6);
                    $arr=array_merge($arr,$arr5);
                    $arr=array_merge($arr,$arr4);
                    $arr=array_merge($arr,$arr3);
                    $arr=array_merge($arr,$arr2);
                    $arr=array_merge($arr,$arr1);
                }else{
                    return '投注方式错误';
                }
            }

            //结算
            $count=count($arr);//中奖注数
            $allmoney=array_sum($arr);//总奖金
            $amount=$allmoney*0.9; //用户实获奖金
            $bmount=$allmoney*0.07; //被跟单用户奖金
            $cmount=$allmoney*0.03; //系统盈利
            //开启事务

            Db::startTrans();
            try {

            if($allmoney==0&&$count==0){
                $a=[
                    'isdraw'=>3,
            ];
                Db::name('order')->where('isdraw',1)->where('id',$date[$k]['id'])->update($a); //更新订单状态
            }else {
                $a = [
                    'isdraw' => 2,
                    'mode' => $allmoney,
                    'okamount' => $amount,
                    'amount' => $count,
                ];

                if ($date[$k]['type'] == 1) { //该订单为跟单时需返奖

                    $c = Db::name('user')->field('user,name,money,blockmoney')->where('id', $date[$k]['uid'])->find();
                    $b = [
                        'order_id' => $date[$k]['trano'],
                        'uid' => $date[$k]['uid'],
                        'type' => 2,
                        'username' => $c['user'],
                        'caicontent' => '返奖奖金',
                        'caizhong' => '足彩',
                        'name' => $c['name'],
                        'money' => $c['money'],
                        'lastmoney' => $c['money'] + $amount,
                        'dmoney' => $c['blockmoney'],
                        'time' => date('Y-m-d H:i:s'),
                    ];
                    Db::name('order')->where('isdraw', 1)->where('id', $date[$k]['id'])->update($a); //更新订单状态
                    Db::name('user')->where('id', $date[$k]['uid'])->setInc('money', $amount); //更新该用户余额状态
                    Db::name('user')->where('id', $date[$k]['uid'])->setInc('tx_money', $amount); //更新该用户可用余额状态
                    Db::name('account')->insert($b);  //账变记录

                    //被跟单用户的返奖

                    $d = Db::name('user')->field('user,name,money,blockmoney')->where('id', $date[$k]['pid'])->find();
                    $dd = [
                        'order_id' => $date[$k]['trano'],
                        'uid' => $date[$k]['pid'],
                        'type' => 2,
                        'username' => $d['user'],
                        'caicontent' => '跟单用户返奖奖金',
                        'caizhong' => '足彩',
                        'name' => $d['name'],
                        'money' => $d['money'],
                        'lastmoney' => $d['money'] + $bmount,
                        'dmoney' => $d['blockmoney'],
                        'time' => date('Y-m-d H:i:s'),
                    ];
                    Db::name('user')->where('id', $date[$k]['pid'])->setInc('money', $bmount); //更新该用户余额状态
                    Db::name('user')->where('id', $date[$k]['pid'])->setInc('tx_money', $bmount); //更新该用户可用余额状态
                    Db::name('account')->insert($dd);  //账变记录

                    //系统返奖
                    $t=[
                      'aid'=>is_signin(),
                        'user'=>$c['user'],
                        'order_id'=>$date[$k]['trano'],
                        'allmoney'=>$allmoney,
                        'money'=>$cmount,
                        'time' => date('Y-m-d H:i:s'),
                    ];
                    Db::name('admin_user')->where('id',is_signin())->setInc('money', $cmount); //更新该系统可用余额状态
                    Db::name('admin_account')->insert($t); //账变记录

                } else {  //该订单为认购时
                    $c = Db::name('user')->field('user,name,money,blockmoney')->where('id', $date[$k]['uid'])->find();
                    $b = [
                        'order_id' => $date[$k]['trano'],
                        'uid' => $date[$k]['uid'],
                        'type' => 2,
                        'username' => $c['user'],
                        'caizhong' => '足彩',
                        'caicontent' => '返奖奖金',
                        'name' => $c['name'],
                        'money' => $c['money'],
                        'lastmoney' => $c['money'] + $allmoney,
                        'dmoney' => $c['blockmoney'],
                        'time' => date('Y-m-d H:i:s'),
                    ];
                    $m = [
                        'isdraw' => 2,
                        'mode' => $allmoney,
                        'okamount' => $allmoney,
                        'amount' => $count,
                    ];
                    Db::name('order')->where('isdraw', 1)->where('id', $date[$k]['id'])->update($m); //更新订单状态
                    Db::name('user')->where('id', $date[$k]['uid'])->setInc('money', $allmoney); //更新该用户余额状态
                    Db::name('user')->where('id', $date[$k]['uid'])->setInc('tx_money', $allmoney); //更新该用户可用余额状态
                    Db::name('account')->insert($b);  //账变记录
                }
            }

                Db::commit();  // 提交事务
                return returnOk(1);

            }catch (\Exception $e) {
                Db::rollback();   // 回滚事务
                return false;
            }
        }

    }
    public function two($date,$bei,$price){
        $cont=[];
        for($i=0;$i<count($date);$i++) {   //3场3串3
            for ($j = $i + 1; $j < count($date); $j++) {

                for ($k = 0; $k < count($date[$i]); $k++) {
                    for ($kv = 0; $kv < count($date[$j]); $kv++) {
                        $cont[] = round($date[$i][$k] * $date[$j][$kv] * $price* $bei ,2);
                    }
                }
            }
        }
        return $cont;
    }
    public function tree($date,$bei,$price){
        $cont=[];
        for($i=0;$i<count($date);$i++) {  //4场4串4
            for ($j = $i + 1; $j < count($date); $j++) {
                for ($v = $j + 1; $v < count($date); $v++) {

                    for ($k = 0; $k < count($date[$i]); $k++) {
                        for ($kv = 0; $kv < count($date[$j]); $kv++) {
                            for ($kv2 = 0; $kv2 < count($date[$v]); $kv2++) {
                                $cont[] = round($date[$i][$k] * $date[$j][$kv]* $date[$v][$kv2] * $price* $bei ,2);
                            }
                        }
                    }
                }
            }
        }
        return $cont;
    }
    public function four($date,$bei,$price){
        $cont=[];
        for($i=0;$i<count($date);$i++) {    //5串5   --4个一组
            for ($j = $i + 1; $j < count($date); $j++) {
                for ($v = $j + 1; $v < count($date); $v++) {
                    for ($v3 = $v + 1; $v3 < count($date); $v3++) {

                        for ($k = 0; $k < count($date[$i]); $k++) {
                            for ($kv = 0; $kv < count($date[$j]); $kv++) {
                                for ($kv2 = 0; $kv2 < count($date[$v]); $kv2++) {
                                    for ($kv3 = 0; $kv3 < count($date[$v3]); $kv3++) {
                                        $cont[] = round($date[$i][$k] * $date[$j][$kv] * $date[$v][$kv2]* $date[$v3][$kv3] * $price* $bei ,2);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return $cont;
    }
    public function five($date,$bei,$price){
        $cont=[];
        for($i=0;$i<count($date);$i++) {    //6串6   --5个一组
            for ($j = $i + 1; $j < count($date); $j++) {
                for ($v = $j + 1; $v < count($date); $v++) {
                    for ($v3 = $v + 1; $v3 < count($date); $v3++) {
                        for ($v4 = $v3 + 1; $v4 < count($date); $v4++) {

                            for ($k = 0; $k < count($date[$i]); $k++) {
                                for ($kv = 0; $kv < count($date[$j]); $kv++) {
                                    for ($kv2 = 0; $kv2 < count($date[$v]); $kv2++) {
                                        for ($kv3 = 0; $kv3 < count($date[$v3]); $kv3++) {
                                            for ($kv4 = 0; $kv4 < count($date[$v4]); $kv4++) {
                                                $cont[] = round($date[$i][$k] * $date[$j][$kv] * $date[$v][$kv2] * $date[$v3][$kv3]* $date[$v4][$kv4] * $price* $bei ,2);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return $cont;
    }
    public function six($date,$bei,$price){
        $cont=[];
        for($i=0;$i<count($date);$i++) {    //7串7   --6个一组
            for ($j = $i + 1; $j < count($date); $j++) {
                for ($v = $j + 1; $v < count($date); $v++) {
                    for ($v3 = $v + 1; $v3 < count($date); $v3++) {
                        for ($v4 = $v3 + 1; $v4 < count($date); $v4++) {
                            for ($v5 = $v4 + 1; $v5 < count($date); $v5++) {

                                for ($k = 0; $k < count($date[$i]); $k++) {
                                    for ($kv = 0; $kv < count($date[$j]); $kv++) {
                                        for ($kv2 = 0; $kv2 < count($date[$v]); $kv2++) {
                                            for ($kv3 = 0; $kv3 < count($date[$v3]); $kv3++) {
                                                for ($kv4 = 0; $kv4 < count($date[$v4]); $kv4++) {
                                                    for ($kv5 = 0; $kv5 < count($date[$v5]); $kv5++) {
                                                        $cont[] = round($date[$i][$k] * $date[$j][$kv] * $date[$v][$kv2] * $date[$v3][$kv3] * $date[$v4][$kv4]* $date[$v5][$kv5] * $price* $bei ,2);
                                                    }
                                                }
                                            }
                                        }
                                    }}
                            }
                        }
                    }
                }
            }
        }
        return $cont;
    }
    public function seven($date,$bei,$price){
        $cont=[];
        for($i=0;$i<count($date);$i++) {    //8串8   --7个一组
            for ($j = $i + 1; $j < count($date); $j++) {
                for ($v = $j + 1; $v < count($date); $v++) {
                    for ($v3 = $v + 1; $v3 < count($date); $v3++) {
                        for ($v4 = $v3 + 1; $v4 < count($date); $v4++) {
                            for ($v5 = $v4 + 1; $v5 < count($date); $v5++) {
                                for ($v6 = $v5 + 1; $v6 < count($date); $v6++) {

                                    for ($k = 0; $k < count($date[$i]); $k++) {
                                        for ($kv = 0; $kv < count($date[$j]); $kv++) {
                                            for ($kv2 = 0; $kv2 < count($date[$v]); $kv2++) {
                                                for ($kv3 = 0; $kv3 < count($date[$v3]); $kv3++) {
                                                    for ($kv4 = 0; $kv4 < count($date[$v4]); $kv4++) {
                                                        for ($kv5 = 0; $kv5 < count($date[$v5]); $kv5++) {
                                                            for ($kv6 = 0; $kv6 < count($date[$v6]); $kv6++) {
                                                                $cont[] = round($date[$i][$k] * $date[$j][$kv] * $date[$v][$kv2] * $date[$v3][$kv3] * $date[$v4][$kv4] * $date[$v5][$kv5]* $date[$v6][$kv6] * $price* $bei ,2);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return $cont;
    }
}